package com.epoch.wx.util;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.jfinal.kit.PropKit;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;

public class WechatUtil {
	
	private static WxMaConfig wxMaConfig ;
	
	private static WxMaService wxMaService;
	
	private static WxPayConfig wxPayConfig;
	
	private static WxPayService wxPayService;
	
	public static WxMaConfig getWxMaConfig() {
		if(null == wxMaConfig) {
			wxMaConfig = new WxMaInMemoryConfig();
			((WxMaInMemoryConfig) wxMaConfig).setAppid(PropKit.use("application.properties").get("wx.appId"));
			((WxMaInMemoryConfig) wxMaConfig).setSecret(PropKit.use("application.properties").get("wx.secret"));
			((WxMaInMemoryConfig) wxMaConfig).setToken(PropKit.use("application.properties").get("wx.token"));
			((WxMaInMemoryConfig) wxMaConfig).setAesKey(PropKit.use("application.properties").get("wx.aesKey"));
			((WxMaInMemoryConfig) wxMaConfig).setMsgDataFormat(PropKit.use("application.properties").get("wx.msgDataFormat"));
		}
		return wxMaConfig;
	}
	
	public static WxPayConfig getWxPayConfig() {
		if(null == wxPayConfig) {
			wxPayConfig = new WxPayConfig();
			wxPayConfig.setAppId(PropKit.use("application.properties").get("wx.appId"));
			wxPayConfig.setMchId(PropKit.use("application.properties").get("wx.mchId"));
			wxPayConfig.setMchKey(PropKit.use("application.properties").get("wx.mchKey"));
			wxPayConfig.setKeyPath(PropKit.use("application.properties").get("wx.certName"));
			wxPayConfig.setTradeType(PropKit.use("application.properties").get("wx.tradeType"));
			wxPayConfig.setNotifyUrl(PropKit.use("application.properties").get("wx.notifyUrl"));
		}
		return wxPayConfig;
	}
	
	public static WxMaService getWxMaService() {
		if(null == wxMaService) {
			wxMaService = new WxMaServiceImpl();
			wxMaService.setWxMaConfig(getWxMaConfig());
		}
		return wxMaService;
	}
	
	public static WxPayService getWxPayService() {
		if(null == wxPayService) {
			wxPayService = new WxPayServiceImpl();
			wxPayService.setConfig(getWxPayConfig());
		}
		return wxPayService;
	}
}
